package ik.xcj.hostpital.manage.controller;

import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import ik.xcj.hostpital.manage.pojo.dto.DepartmentAddNewDTO;
import ik.xcj.hostpital.manage.pojo.dto.DepartmentUpdateDTO;
import ik.xcj.hostpital.manage.pojo.dto.PatientAddNewDTO;
import ik.xcj.hostpital.manage.pojo.dto.PatientUpdateDTO;
import ik.xcj.hostpital.manage.pojo.vo.DepartmentListItemVO;
import ik.xcj.hostpital.manage.pojo.vo.DepartmentStandardVO;
import ik.xcj.hostpital.manage.pojo.vo.PatientListItemVO;
import ik.xcj.hostpital.manage.pojo.vo.PatientStandardVO;
import ik.xcj.hostpital.manage.service.IDepartmentService;
import ik.xcj.hostpital.manage.service.IPatientService;
import ik.xcj.hostpital.manage.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理就诊人相关请求的控制器
 *
 * @author java@ik.xcj
 * @version 0.0.1
 */
@Slf4j
@RestController
@Api(tags = "就诊人管理模块")
@RequestMapping("/patient")
public class PatientController {

    @Autowired
    private IPatientService patientService;

    public PatientController() {
        log.debug("创建控制器类对象：PatientController");
    }

    @PostMapping("/add-new")
    @ApiOperation("添加就诊人")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid PatientAddNewDTO patientAddNewDTO){
        log.debug("开始处理【添加就诊人】的请求，参数：{}",patientAddNewDTO);
        patientService.addNew(patientAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除就诊人")
    @ApiOperationSupport(order = 200)
    public JsonResult delete(@Range(min = 1, max = 1000000, message = "删除就诊人失败，就诊人ID非法！")
                             @PathVariable Long id){
        log.debug("开始处理【根据id删除就诊人】的请求，参数：{}",id);
        patientService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改就诊人数据")
    @ApiOperationSupport(order = 300)
    public JsonResult updateInfoById(@PathVariable Long id, PatientUpdateDTO patientUpdateDTO) {
        log.debug("开始处理【修改就诊人详情】的请求，参数：{}", patientUpdateDTO);
        patientService.updateInfoById(id, patientUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据id查询就诊人详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id", value = "就诊人ID", required = true, example = "1", dataType = "long")
    public JsonResult<PatientStandardVO> getStandardById(@Range(min = 1, max = 1000000, message = "查询就诊人详情失败，就诊人ID非法！")
                                                         @PathVariable Long id) {
        log.debug("开始处理【根据id查询就诊人详情】的请求，参数：{}", id);
        PatientStandardVO patientStandardById = patientService.getStandardById(id);
        return JsonResult.ok(patientStandardById);
    }

    @GetMapping("/page")
    @ApiOperation("查询就诊人列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<PageInfo<PatientListItemVO>> list(Integer page,Integer pageSize) {
        log.debug("开始处理【查询就诊人列表】的业务，参数：{},{}",page,pageSize);
        PageInfo<PatientListItemVO> list = patientService.list(page, pageSize);
        return JsonResult.ok(list);
    }

}
